package org.jeecg.modules.demo.yuedupeihe.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.peihebumenbiao.entity.JieshouDto;
import org.jeecg.modules.demo.peihebumenbiao.entity.Peihebumenbiao;
import org.jeecg.modules.demo.yuedupeihe.entity.PeiheDto;
import org.jeecg.modules.demo.yuedupeihe.entity.Yuedupeihebumenbiao;
import org.jeecg.modules.demo.yuedupeihe.service.IYuedupeihebumenbiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.yuedushenhe.entity.Yuedushenhefankuibiao;
import org.jeecg.modules.demo.yuedushenhe.service.IYuedushenhefankuibiaoService;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.entity.Zhidingyueduzhongdiangongzuo;
import org.jeecg.modules.demo.zhidingyueduzhongdiangongzuo.service.IZhidingyueduzhongdiangongzuoService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserDepart;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 配合部门表
 * @Author: jeecg-boot
 * @Date:   2025-11-08
 * @Version: V1.0
 */
@Tag(name="配合部门表")
@RestController
@RequestMapping("/yuedupeihe/yuedupeihebumenbiao")
@Slf4j
public class YuedupeihebumenbiaoController extends JeecgController<Yuedupeihebumenbiao, IYuedupeihebumenbiaoService> {
	@Autowired
	private IYuedupeihebumenbiaoService yuedupeihebumenbiaoService;
	@Autowired
	private ISysUserRoleService sysUserRoleService;
	@Autowired
	private IZhidingyueduzhongdiangongzuoService zhidingyueduzhongdiangongzuoService;
	@Autowired
	private IYuedushenhefankuibiaoService yuedushenhefankuibiaoService;
	@Autowired
	private ISysUserDepartService sysUserDepartService;
	/**
	 * 分页列表查询
	 *
	 * @param yuedupeihebumenbiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "配合部门表-分页列表查询")
//	@Operation(summary="配合部门表-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Yuedupeihebumenbiao>> queryPageList(Yuedupeihebumenbiao yuedupeihebumenbiao,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Yuedupeihebumenbiao> queryWrapper = QueryGenerator.initQueryWrapper(yuedupeihebumenbiao, req.getParameterMap());
//		Page<Yuedupeihebumenbiao> page = new Page<Yuedupeihebumenbiao>(pageNo, pageSize);
//		IPage<Yuedupeihebumenbiao> pageList = yuedupeihebumenbiaoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
	 //list接口，需要根据是部长还是主管返回不同的数据
	 @GetMapping(value = "/list")
	 public Result<IPage<PeiheDto>> queryPageList(Yuedupeihebumenbiao yuedupeihebumenbiao,
												 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												 HttpServletRequest req) {
		 //获取登录用户
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String userid = sysUser.getId();
		 QueryWrapper<SysUserRole> queryWrapper1 = new QueryWrapper<>();
		 queryWrapper1.eq("user_id",userid);
		 SysUserRole sysUserRole = sysUserRoleService.getOne(queryWrapper1);
		 String roleId = sysUserRole.getRoleId();
		 IPage<PeiheDto> pageDto = new Page<>();
		 List<PeiheDto> listDto = new ArrayList<>();
		 //角色为部长的数据
		 if(roleId.equals("1970852625298092033")){
			 Page<Yuedupeihebumenbiao> page = new Page<>();
			 QueryWrapper<Yuedupeihebumenbiao> queryWrapper2 = new QueryWrapper<>();
			 queryWrapper2.eq("peihebumen",sysUser.getDepartIds());
			 queryWrapper2.eq("zhuangtai","部长待接收");
			 IPage<Yuedupeihebumenbiao> pageList = yuedupeihebumenbiaoService.page(page, queryWrapper2);
			 List<Yuedupeihebumenbiao> list1 = pageList.getRecords();
			 if(list1.size()>0){
				 for(int i = 0;i<list1.size();i++){
					 PeiheDto peiheDto = new PeiheDto();
					 BeanUtils.copyProperties(list1.get(i),peiheDto);
					 peiheDto.setGongzuoneirong(list1.get(i).getXiezuoneirong());
					 Zhidingyueduzhongdiangongzuo zhidingyueduzhongdiangongzuo = zhidingyueduzhongdiangongzuoService.getById(list1.get(i).getZhidingid());
					 peiheDto.setGuikoubumen(zhidingyueduzhongdiangongzuo.getZhuguanbumen());
					 listDto.add(peiheDto);
				 }
			 }
			 pageDto.setRecords(listDto);
		 }
		 //角色为主管的数据
		 else if(roleId.equals("1976183850854342657")){
			 //配合的数据
			 Page<Yuedushenhefankuibiao> fankuiPage = new Page<>();
			 QueryWrapper<SysUserDepart> qw = new QueryWrapper<>();
			 qw.eq("user_id",userid);
			 SysUserDepart sysUserDepart = sysUserDepartService.getOne(qw);
			 QueryWrapper<Yuedushenhefankuibiao> queryWrapper3 = new QueryWrapper<>();
			 queryWrapper3.eq("peihebumen",sysUserDepart.getDepId());
			 queryWrapper3.eq("zhuangtai","进行中");
			 IPage<Yuedushenhefankuibiao> fankuiPageList = yuedushenhefankuibiaoService.page(fankuiPage, queryWrapper3);
			 List<Yuedushenhefankuibiao> list4 = fankuiPageList.getRecords();
			 if(list4 != null && !list4.isEmpty()){
				 for(int i = 0;i<list4.size();i++){
					 PeiheDto peiheDto = new PeiheDto();
					 BeanUtils.copyProperties(list4.get(i),peiheDto);
					 peiheDto.setId(list4.get(i).getZhidingid());
					 peiheDto.setShenheid(list4.get(i).getShenheid());
					 Yuedupeihebumenbiao yueduph = yuedupeihebumenbiaoService.getById(list4.get(i).getShenheid());
					 peiheDto.setGongzuoneirong(yueduph.getXiezuoneirong());
					 peiheDto.setZhiliangyaoqiu(yueduph.getZhiliangyaoqiu());
					 peiheDto.setWanchengshijian(yueduph.getWanchengshijian());
					 Zhidingyueduzhongdiangongzuo zhidingyueduzhongdiangongzuo = zhidingyueduzhongdiangongzuoService.getById(list4.get(i).getZhidingid());
					 peiheDto.setGuikoubumen(zhidingyueduzhongdiangongzuo.getZhuguanbumen());
					 peiheDto.setZhuguanpeihe("配合");
					 listDto.add(peiheDto);
				 }
				 pageDto.setRecords(listDto);
			 }
			 //主管的数据
			 Page<Zhidingyueduzhongdiangongzuo> zhidingPage = new Page<>();
			 QueryWrapper<Zhidingyueduzhongdiangongzuo> queryWrapper4 = new QueryWrapper<>();
			 queryWrapper4.eq("zerenzhuguan",userid);
			 queryWrapper4.eq("zhuangtai","进行中");
			 IPage<Zhidingyueduzhongdiangongzuo> zhidingPageList = zhidingyueduzhongdiangongzuoService.page(zhidingPage, queryWrapper4);
			 List<Zhidingyueduzhongdiangongzuo> list5 = zhidingPageList.getRecords();
			 if(list5 != null && !list5.isEmpty()){
				 for(int i = 0;i<list5.size();i++){
					 PeiheDto peiheDto = new PeiheDto();
					 BeanUtils.copyProperties(list5.get(i),peiheDto);
					 peiheDto.setGuikoubumen(list5.get(i).getZhuguanbumen());
					 peiheDto.setZhuguanpeihe("主管");
					 listDto.add(peiheDto);
				 }
			 }
			 pageDto.setRecords(listDto);
		 }
		 return Result.OK(pageDto);
	 }


	 //配合主管
	 @GetMapping(value = "/peihezhuguan")
	 public Result<List<SysUser>> peihezhuguan(){
		 List<SysUser> list =yuedupeihebumenbiaoService.getUserList();
		 return Result.OK(list);
	 }

	 //选择配合主管
	 @PostMapping(value="/chosoePeihezhuguan")
	 public Result<String> chosoepeihezhuguan(@RequestBody PeiheDto peiheDto){
		try{
			yuedupeihebumenbiaoService.choosePeihezhuguan(peiheDto);
			return Result.OK("选择成功！");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	 }

	 //接收驳回
	@PostMapping(value = "/jieshou")
	 public Result<String> jieshou(@RequestBody PeiheDto peiheDto) {
		try {
			yuedupeihebumenbiaoService.receive(peiheDto);
			return Result.OK("接收成功！");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	/**
	 *   添加
	 *
	 * @param yuedupeihebumenbiao
	 * @return
	 */
	@AutoLog(value = "配合部门表-添加")
	@Operation(summary="配合部门表-添加")
	@RequiresPermissions("yuedupeihe:z_peihebumenbiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Yuedupeihebumenbiao yuedupeihebumenbiao) {
		yuedupeihebumenbiaoService.save(yuedupeihebumenbiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param yuedupeihebumenbiao
	 * @return
	 */
	@AutoLog(value = "配合部门表-编辑")
	@Operation(summary="配合部门表-编辑")
	@RequiresPermissions("yuedupeihe:z_peihebumenbiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Yuedupeihebumenbiao yuedupeihebumenbiao) {
		yuedupeihebumenbiaoService.updateById(yuedupeihebumenbiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "配合部门表-通过id删除")
	@Operation(summary="配合部门表-通过id删除")
	@RequiresPermissions("yuedupeihe:z_peihebumenbiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		yuedupeihebumenbiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "配合部门表-批量删除")
	@Operation(summary="配合部门表-批量删除")
	@RequiresPermissions("yuedupeihe:z_peihebumenbiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yuedupeihebumenbiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "配合部门表-通过id查询")
	@Operation(summary="配合部门表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Yuedupeihebumenbiao> queryById(@RequestParam(name="id",required=true) String id) {
		Yuedupeihebumenbiao yuedupeihebumenbiao = yuedupeihebumenbiaoService.getById(id);
		if(yuedupeihebumenbiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yuedupeihebumenbiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yuedupeihebumenbiao
    */
    @RequiresPermissions("yuedupeihe:z_peihebumenbiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Yuedupeihebumenbiao yuedupeihebumenbiao) {
        return super.exportXls(request, yuedupeihebumenbiao, Yuedupeihebumenbiao.class, "配合部门表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("yuedupeihe:z_peihebumenbiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Yuedupeihebumenbiao.class);
    }

}
